﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using Microsoft.Build.Evaluation;
using Microsoft.Xna.Framework.Content;

namespace TinyEngine.Editor.Extensibility
{
    public class TinyProject
    {
        [XmlIgnore]
        public ContentManager Content { get; set; }

        public string Name { get; set; }

        public string CSProjectFile { get; set; }

        [XmlIgnore]
        public string GameExe { get; set; }

        [XmlIgnore]
        public string FullPath{ get; set; }

        [XmlIgnore]
        public string RootPath { get; set; }
        
        [XmlIgnore]
        public string ContentPath { get; set; }

        [XmlIgnore]
        public string ContentPathRuntime { get; set; }



        internal void Save(string filename)
        {
            var writer = XmlWriter.Create(filename);
            XmlSerializer ser = new XmlSerializer(typeof(TinyProject));

            ser.Serialize(writer, this);

            writer.Close();
        }

        internal static TinyProject Load(string filename)
        {
            var reader=new StreamReader(filename);
            XmlSerializer ser = new XmlSerializer(typeof(TinyProject));
            var obj = ser.Deserialize(reader);
            TinyProject proj = obj as TinyProject;
            proj.FullPath = filename;
            proj.RootPath = Path.GetDirectoryName(filename);

            var projPath = Path.Combine(proj.RootPath, proj.CSProjectFile);
            Microsoft.Build.Evaluation.Project vsproj = new Microsoft.Build.Evaluation.Project(projPath);
            //_p.Build();
            string str = vsproj.GetPropertyValue("AssemblyName");
            proj.GameExe = str + ".exe";

            SetContentPath(proj, vsproj);
            return proj;
        }

        private static void SetContentPath(TinyProject proj, Microsoft.Build.Evaluation.Project vsproj)
        {
            foreach (var item in vsproj.AllEvaluatedItems)
            {
                if (item.ItemType == "ProjectReference")
                {
                    ProjectMetadata previousMetadata = null;
                    foreach (var metadata in item.Metadata)
                    {
                        if (metadata.Name == "XnaReferenceType" && metadata.EvaluatedValue == "Content")
                        {
                            if (previousMetadata == null)
                            {
                                throw new Exception("Found XnaContent Project reference but Metadata order is incorrect");
                            }
                            proj.ContentPath = Path.GetDirectoryName(item.UnevaluatedInclude); 
                            proj.ContentPathRuntime = "Content";

                        }
                        previousMetadata = metadata;
                    }
                }
            }

        }
    }
}
